import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { ConfigModule } from './common/config/config.module';
import { LogsModule } from './common/logger/logs.module';
import { CacheModule } from '@nestjs/cache-manager';
import { Keyv } from 'keyv';
import { createKeyv } from '@keyv/redis';
// import { RedisStore } from 'cache-manager-ioredis-yet';
import { CacheableMemory } from 'cacheable';
import { MailModule } from './common/mail/mail.module';
import { PrismaModule } from './database/prisma/prisma.module';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config/dist/config.service';
import { User } from './database/user/user.entity';
import { MongooseModule } from '@nestjs/mongoose';
import { UserRepository } from './user/user.repository';
import { AppService } from 'app.service';

const cacheModule = CacheModule.registerAsync({
    useFactory: () => {
        const kvStore = new Keyv({ store: new CacheableMemory({ ttl: 5000, lruSize: 5000 }) });
        return {
            stores: [kvStore, createKeyv('redis://localhost:7001')],
            ttl: 5000,
        };
    },
});

@Module({
    imports: [
        ConfigModule,
        LogsModule,
        cacheModule,
        MailModule,
        PrismaModule,
        TypeOrmModule.forRootAsync({
            inject: [ConfigService],
            useFactory: (config: ConfigService, appService: AppService) => {
                return {
                    type: config.get('DB_TYPE'),
                    host: config.get('DB_HOST'),
                    port: 3306,
                    username: config.get('DB_USERNAME'),
                    password: config.get('DB_PASSWORD'),
                    database: config.get('DB_DATABASE'),
                    autoLoadEntities: Boolean(config.get<string>('DB_AUTOLOAD', 'false')),
                    synchronize: Boolean(config.get<string>('DB_SYNC', 'false')),
                } as TypeOrmModuleOptions;
            },
            extraProviders: [AppService],
        }),
        TypeOrmModule.forRootAsync({
            name: 'mysql2',
            inject: [ConfigService],
            useFactory: (config: ConfigService) => {
                return {
                    type: config.get('DB_TYPE'),
                    host: config.get('DB_HOST'),
                    port: 3307,
                    username: config.get('DB_USERNAME'),
                    password: config.get('DB_PASSWORD'),
                    database: config.get('DB_DATABASE'),
                    autoLoadEntities: Boolean(config.get<string>('DB_AUTOLOAD', 'false')),
                    synchronize: Boolean(config.get<string>('DB_SYNC', 'false')),
                } as TypeOrmModuleOptions;
            },
        }),
        TypeOrmModule.forFeature([User]),

        TypeOrmModule.forFeature([User], 'mysql2'),
        MongooseModule.forRoot('mongodb://localhost:17017/test'),
    ],
    controllers: [AppController],
    providers: [UserRepository],
})
export class AppModule {}
